<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view @tap="registerFun" class="registerView"><text>注册</text></view>
			
			<img class="myimg" src="static/imgs/1.png" alt="" srcset="" />
			<!-- <view class="header">
				<image :src="logoImage"></image>
			</view> -->
			<!-- 主体表单 -->
			<view class="main">
				<wInput
					v-model="phoneData"
					type="text"
					maxlength="11"
					placeholder="请输入用户名"
				></wInput>
				<wInput
					v-model="passData"
					type="password"
					maxlength="11"
					placeholder="请输入密码"
				></wInput>
			</view>
			
			<wButton 
				text="登 录"
				:rotate="isRotate" 
				@click.native="startLogin()"
				class="wbutton"
				bg-color="#296049"
			></wButton>
			
			<!-- 其他登录 -->
			<view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-weibo" @tap="login_weibo"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-github" @tap="login_github"></view>
				</view>
			</view>

			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view>
			
		</view>
	</view>
</template>

<script>
	var _this;
	import wInput from '../../../components/watch-login/watch-input.vue'; //输入框
	import wButton from '../../../components/watch-login/watch-button.vue'; //按钮
	import {
		getLogin,
		wexinLogin,
	} from '../../../service/api/login.js' //登录api
import { login } from '../../../service/api/api';
	
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				phoneData:'', //用户/电话
				passData:'', //密码
				isRotate: false, //是否启动加载
				
			};
		},
		components:{
			wInput,
			wButton
		},
		mounted() {
			_this= this;
			this.isLogin();
		},
		methods: {
			registerFun(){
				uni.reLaunch({
					url: "../../../pages/mine/children/register",
				});	
			},
			async isLogin(){
				//判断缓存中是否登录过，直接登录
				try {
					const value = uni.getStorageSync('setUserData');
					if (value) {
						//有登录信息
						console.log("已登录用户：",value);
						_this.$store.dispatch("setUserData",value); //存入状态
						uni.navigateTo({
							url: '../../../pages/index',
						});
					}
					
				
				
				} catch (e) {
					// error
				}
			},
		   async startLogin(){
				//登录
				
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				
				if (this.phoneData.length == "") {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '用户名不能为空'
				    });
				    return;
				}
		        if (this.passData.length < 5) {
		            uni.showToast({
		                icon: 'none',
						position: 'bottom',
		                title: '密码不正确'
		            });
		            return;
		        }
				// uni.showLoading({
				// 	tittle: '登录中'
				// });
				_this.isRotate=true
				
				if(this.passData=="admin" && this.phoneData=="admin"){
					setTimeout(function(){
								let userdata={
									"username":"admin",
									"nickname":"admin",
									"accesstoken":"c0a93ef40df7eec23db074266b1ac0e8",
								}
								_this.$store.dispatch("setUserData",userdata); //存入状态
								// try {
									uni.setStorageSync('setUserData', JSON.stringify(userdata) ); //存入缓存
								// } catch (e) {
								// 	// error
								// }
								_this.isRotate=false
								uni.showToast({
									icon: 'success',
									position: 'bottom',
									title: '登录成功'
								});
								uni.reLaunch({
									url: '../../../pages/index',
								});
							
							uni.hideLoading();
						
					},2000)
					
					
				}else{
					
					let result = 	await login({"password": this.passData,
					"username": this.phoneData});
					console.log("登录的结果是：",result);
					if(result.code==200){
						//这里加了个延时，主要是为了让登录动画完整显示出来， 不需要可以删除
						setTimeout(function(){
									let userdata={
										"username":this.phoneData,
										"nickname":this.phoneData,
										"accesstoken":"c0a93ef40df7eec23db074266b1ac0e8",
									}
									_this.$store.dispatch("setUserData",userdata); //存入状态
									// try {
										uni.setStorageSync('setUserData', JSON.stringify(userdata)); //存入缓存
									// } catch (e) {
									// 	// error
									// }
									_this.isRotate=false
									uni.showToast({
										icon: 'success',
										position: 'bottom',
										title: '登录成功'
									});
									uni.reLaunch({
										url: '../../../pages/index',
									});
								
								uni.hideLoading();
							
						},2000)
						
					}else{
						uni.showToast({
							icon: 'none',
							position: 'bottom',
							title: result.msg
						});
						setTimeout(()=>{
							uni.hideLoading();
							_this.isRotate=false
						},2000)
					}
					
					
				}
				
				
		    },
			login_weixin() {
				//微信登录（没有服务器 测试不了，就做到这一步没了哈哈哈哈）
				// #ifdef MP-WEIXIN
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '无服务器进行测试...'
				});
				// const appid=''; //小程序id
				// const secret=''; //小程序 appSecret
				// 
				// let jscode=''; //登录时获取的 code
				// wx.login({
				// 	success (res) {
				// 		if (res.code) {
				// 			console.log(res.code);
				// 			jscode = res.code;
				// 			//微信登录
				// 			wexinLogin(appid,secret,jscode)
				// 			.then(res => {
				// 				console.log(res)
				// 			}).catch(err => {
				// 				console.log(err)
				// 				
				// 			})
				// 			
				// 		} else {
				// 		  console.log('登录失败！' + res.errMsg)
				// 		}
				// 	}
				// })
				
				// #endif
				// #ifndef MP-WEIXIN
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '...'
					});
				// #endif
			},
			login_weibo() {
				//微博登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_github() {
				//github登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			}
			
		}
	}
</script>

<style>
	@import url("../css/main.css");
	.login {
		height: 100vh;
		background-color: #fefdf3;
		padding-top: 50px;
	}
</style>
